package com.logicalthinking.socialmall.biz;

import com.github.chenlijia1111.utils.common.Result;
import com.github.chenlijia1111.utils.common.constant.BooleanConstant;
import com.github.chenlijia1111.utils.database.mybatis.pojo.PageInfo;
import com.github.chenlijia1111.utils.list.Lists;
import com.logicalthinking.socialmall.common.vo.requestVo.role.RoleAddParams;
import com.logicalthinking.socialmall.common.vo.requestVo.role.RolePageQueryParams;
import com.logicalthinking.socialmall.common.vo.requestVo.role.RoleUpdateParams;
import com.logicalthinking.socialmall.common.vo.responseVo.role.RoleVo;
import com.logicalthinking.socialmall.entity.Role;
import com.logicalthinking.socialmall.service.RoleServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 角色表
 *
 * @author chenLiJia
 * @since 2020-02-17 14:43:59
 **/
@Service
public class RoleBiz {

    @Autowired
    private RoleServiceI roleService;//角色

    /**
     * 添加角色
     *
     * @param params
     * @return
     */
    public Result add(RoleAddParams params) {

        //校验身份

        //判断角色名称是否已存在
        Role roleCondition = new Role().setRoleName(params.getRoleName()).
                setDeleteStatus(BooleanConstant.NO_INTEGER);
        List<Role> roles = roleService.listByCondition(roleCondition);
        if (Lists.isNotEmpty(roles)) {
            return Result.failure("角色名称已存在");
        }

        //转换数据
        Role role = new Role().setRoleName(params.getRoleName()).setRoleDesc(params.getRoleDesc()).
                setCreateTime(new Date()).setUpdateTime(new Date()).
                setDeleteStatus(BooleanConstant.NO_INTEGER);

        roleService.add(role);

        //添加角色权限
        List<Integer> authList = params.getAuthList();
        if (Lists.isNotEmpty(authList)) {
            roleService.batchAddRoleAuth(authList, role.getId());
        }

        return Result.success("操作成功");
    }

    /**
     * 删除角色
     *
     * @param id
     * @return
     */
    public Result delete(Integer id) {

        //判断数据是否存在
        Role roleCondition = new Role().setId(id).setDeleteStatus(BooleanConstant.NO_INTEGER);
        List<Role> roles = roleService.listByCondition(roleCondition);
        if (Lists.isEmpty(roles)) {
            return Result.failure("数据不存在");
        }

        Role role = roles.get(0);
        role.setDeleteStatus(BooleanConstant.YES_INTEGER);

        return roleService.update(role);
    }

    /**
     * 批量删除角色
     *
     * @param idList
     * @return
     */
    @Transactional
    public Result batchDelete(List<Integer> idList) {

        if (Lists.isEmpty(idList)) {
            return Result.failure("id为空");
        }

        for (Integer id : idList) {
            Result result = delete(id);
            if (!result.getSuccess()) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return result;
            }
        }

        return Result.success("操作成功");
    }

    /**
     * 修改角色
     *
     * @param params
     * @return
     */
    public Result update(RoleUpdateParams params) {
        //校验身份

        //判断角色名称是否已存在
        Role roleCondition = new Role().setRoleName(params.getRoleName()).
                setDeleteStatus(BooleanConstant.NO_INTEGER);
        List<Role> roles = roleService.listByCondition(roleCondition);
        if (Lists.isNotEmpty(roles) && roles.stream().filter(e -> !Objects.equals(e.getId(), params.getId())
                && Objects.equals(e.getRoleName(), params.getRoleName())).findAny().isPresent()) {
            return Result.failure("角色名称已存在");
        }

        //转换数据
        Role role = new Role().setRoleName(params.getRoleName()).setRoleDesc(params.getRoleDesc())
                .setUpdateTime(new Date());

        roleService.update(role);

        //删除原来的角色权限
        roleService.deleteRoleAuthByRoleId(params.getId());
        //添加角色权限
        List<Integer> authList = params.getAuthList();
        if (Lists.isNotEmpty(authList)) {
            roleService.batchAddRoleAuth(authList, role.getId());
        }

        return Result.success("操作成功");
    }

    /**
     * 主键查询角色信息
     *
     * @param id
     * @return
     */
    public Result findById(Integer id) {

        Role roleCondition = new Role().setId(id).setDeleteStatus(BooleanConstant.NO_INTEGER);
        List<Role> roles = roleService.listByCondition(roleCondition);
        if (Lists.isEmpty(roles)) {
            return Result.failure("数据不存在");
        }

        Role role = roles.get(0);
        //转化数据
        RoleVo roleVo = new RoleVo();
        roleVo.setId(role.getId()).setRoleName(role.getRoleName()).setRoleDesc(role.getRoleDesc());

        //查询角色
        roleService.getFullInfoWithRoleVo(Lists.asList(roleVo));

        return Result.success("查询成功", roleVo);
    }

    /**
     * 列表查询角色信息
     *
     * @param params
     * @return
     */
    public Result listPage(RolePageQueryParams params) {

        List<RoleVo> roleVos = roleService.listPage(params);
        PageInfo<RoleVo> pageInfo = new PageInfo<>(roleVos);
        return Result.success("查询成功", pageInfo);
    }

}
